<?php
/**
 * @desc 价格字段管理
 * @author kevin wang
 * @date 2014-10-25
 */
class ctrl_price extends ctrl_base {
    private static $dirName = 'price';
    
    /**
     * @desc 价格字段列表
	 * @author kevin wang
	 * @param void
	 * @return void
     */
    public static function main() {
    	if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('price_list'));
		//页面输入参数处理
    	$currentPage = isset($_POST['pageNum']) ? $_POST['pageNum'] : 1;
    	$pagesize = isset($_POST['numPerPage']) ? $_POST['numPerPage'] : self::$_pagesize;
		$nextpage = ($currentPage - 1) * $pagesize;

    	$price_name = isset($_POST['price_name']) ? trim($_POST['price_name']) : '';
		$price_id = isset($_GET['price_id']) ? intval($_GET['price_id']) : 0;
    	//搜索条件组合
		$condition = array();
    	if(!empty($price_name)) {
    		$condition['price_name'] = $price_name;
    	}
		if(!empty($price_id)) {
    		$condition['price_id'] = $price_id;
    	}
		//订单获取
    	$price_list = model_price :: get_price_list($nextpage,$pagesize,$condition);
		$total = array_pop($price_list);
		$price_list = self::build_tree($price_list, 0);
		$price_list = self::count_array_childs($price_list);
    	
		//模板赋值
    	self::smarty() -> assign('price_list', $price_list);
		self::smarty() -> assign('total', $total);
		self::smarty() -> assign('pageNum', $currentPage);
		self::smarty() -> assign('numPerPage', $pagesize);
		self::smarty() -> assign('pageNumShown', self::$_pageNumShown);
		self::smarty() -> assign('price_name', $price_name);
		self::smarty() -> assign('price_id', $price_id);
		self::smarty() -> assign('shoNum', self::$_shoNum);
		
    	self::smarty() -> display(self::$dirName . '/price_list.html');    	
    }
	//添加价格字段
	public static function price_add(){
		if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('price_add'));
		$price_id = isset($_GET['price_id']) ? intval($_GET['price_id']) : 0;
		if($_POST){

			$price_list = self::get_request_data();
			self::check_price($price_list);
			if($price_list['is_show'] == 1){
				$price_list['is_show'] = 0;
			}else{
				$price_list['is_show'] = 1;
			}
			if($price_list['select_all'] == 1){
				$price_list['select_all'] = 0;
			}else{
				$price_list['select_all'] = 1;
			}
			$price_list['price_id'] = $price_id;

			$result = model_price::price_add($price_list);
			if(!empty($result)){
				self::ajax_ouput(200, '操作成功','price_main','closeCurrent');
			}else{
				self::ajax_ouput(300, '添加失败');
			}
		}
		$type = model_produce::get_type_list();
		self::smarty() -> assign("type", $type);
		self::smarty() -> assign("price_id", $price_id);
		self::smarty() -> display(self::$dirName . '/price_add.html');
	}
	//删除价格字段
	public static function price_delete() {
    	if(!model_admin::islogin()) {
			self::goto_login();
    	}
		self::operation(array('price_delete'));
    	$id = intval($_GET['id']);

    	$st = model_price::delete_price($id);

    	if($st > 0) {
			self::ajax_ouput(200, '操作成功','price_main');
    	} else {
			self::ajax_ouput(300, '操作失败,可能该字段已被使用');
    	}
    }
	//编辑价格字段
	 public static function price_edit() {
    	if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		$id = isset($_GET['id'])? (intval($_GET['id'])):(intval($_POST['id']));
    	if($id <= 0) {
			self::ajax_ouput(300, '非法访问');
    	}
		$price_list = model_price :: get_price_list(array(), array(), array());
		$total = array_pop($price_list);

		$price_list = self::build_tree($price_list, 0);
		$price = model_price::get_price_list_by_id($price_list, $id);

		self::operation(array('price_edit'));
		$price_id = isset($_GET['price_id']) ? intval($_GET['price_id']) : 0;
    	if($_POST){
			$price = array();

			$price = self::get_request_data();
			self::check_price($price);
			if($price['is_show'] == 1){
				$price['is_show'] = 0;
			}else{
				$price['is_show'] = 1;
			}
			if($price['select_all'] == 1){
				$price['select_all'] = 0;
			}else{
				$price['select_all'] = 1;
			}
			$price['price_id'] = $price_id;

			$result = model_price::price_edit($id, $price);
	    	
		    if(!empty($result)){
				self::ajax_ouput(200, '操作成功','price_main','closeCurrent');
			}else{
				self::ajax_ouput(300, '操作失败');
			}    		
    	}

    	self::smarty() -> assign('id', $id);
    	self::smarty() -> assign('price', $price);
		self::smarty() -> assign("price_id", $price_id);
    	self::smarty() -> display(self::$dirName . '/price_edit.html');
    }
	//校验价格字段
	public static function check_price(array $price_list){
		if(empty($price_list)){
			self::ajax_ouput(300, '操作失败,价格字段不能为空');
		}
		if(empty($price_list['price_name1'])){
			self::ajax_ouput(300, '操作失败,请正确填写一级/二级字段');
		}
		if(!empty($price_list['price_name3'])){
			foreach($price_list['price_name3'] as $key=>$value){
				foreach($value as $v){
					if(!empty($v) && empty($price_list['price_name2'][$key])){
						self::ajax_ouput(300, '操作失败,请正确填写一级/二级字段');
					}
				}
			}
		}
		return true;
	}
}
